package org.beaconframework.util.xml;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.beaconframework.util.exception.BeaconException;
import org.springframework.context.ApplicationContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class SpringDomSource implements DomSource {

	ApplicationContext ctx;
	String resourceName;
	
	public SpringDomSource(ApplicationContext ctx, String resourceName) {
		this.ctx =  ctx;
		this.resourceName = resourceName;
	}
	@Override
	public Node getNode() {
		try {
		
		Document document = null;
		
			document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(ctx.getResource(resourceName).getInputStream());
			return document;
		} catch (SAXException e) {
			throw new BeaconException(e);
		} catch (IOException e) {
			throw new BeaconException(e);
		} catch (ParserConfigurationException e) {
			throw new BeaconException(e);
		}
		
		
		
	}

}
